<template>
	<view>
		<!-- 使用自定义的搜索组件 -->
		<my-search @doClick="toSearch"></my-search>
		<view class="scroll-view-container">
			<!-- 左侧滑动区域 -->
			<scroll-view class="left-scroll-view" scroll-y="true" :style="{height: wh + 'px'}">
				<view v-for="(item,index) in cateList" :key="index"
					:class="['left-scroll-view-item',index===activeIndex?'active-class':'']"
					@click="activeChange(index)">
					{{item.cat_name}}
				</view>
			</scroll-view>
			<!-- 右侧滑动区域 -->
			<scroll-view scroll-y="true" :style="{height:wh + 'px'}" :scroll-top="rightScrollTop">
				<view class="" v-for="(item2,index2) in cateLevel2">
					<!-- 二级分类的标题 -->
					<view class="cate-lev2-title">
						★★★★★{{item2.cat_name}}★★★★★
					</view>
					<!-- 二级分类下的三级分类的列表 -->
					<view class="cate-lv3-list">
						<!-- 三级分类的item项 -->
						<view class="cate-lv3-item" v-for="(item3,index3) in item2.children" :key="index3"
							@click="toGoodsList(item3)">
							<!-- 三级分类的图片 -->
							<image :src="item3.cat_icon" mode=""></image>
							<!-- 三级分类的文本 -->
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 当前设备的可用高度
				wh: 0,
				// 左侧分类数据列表
				cateList: [],
				// 左侧被激活的分类数据下标，默认是第一个
				activeIndex: 0,
				// 二级分类数据
				cateLevel2: [],
				// 右侧滚动条距离顶部的距离
				rightScrollTop: 0
			}
		},
		methods: {
			async getCateList() {
				let res = await uni.$http.get('/api/public/v1/categories')
				// console.log(res)
				if (res.data.meta.status !== 200) {
					uni.$showMsg()
				}
				this.cateList = res.data.message
				this.cateLevel2 = res.data.message[0].children
			},
			activeChange(index) {
				this.activeIndex = index
				this.cateLevel2 = this.cateList[index].children
				// 重置滚动条距离顶部的距离
				this.rightScrollTop = this.rightScrollTop === 0 ? 1 : 0
			},
			// 点击三级分类的项，跳转到商品列表页面
			toGoodsList(item3) {
				uni.navigateTo({
					// url: "/subpkg/goods_list/goods_list?cid=" + item3.cat_id
					url: `/subpkg/goods_list/goods_list?cid=${item3.cat_id}`
				})
			},
			// 跳转到搜索页面
			toSearch() {
				// console.log("搜索")
				uni.navigateTo({
					url: "/subpkg/search/search"
				})
			}
		},
		onLoad() {
			// 获取设备的可用高度
			let sysInfo = uni.getSystemInfoSync()
			// console.log(sysInfo)
			this.wh = sysInfo.windowHeight - 50

			// 获取分类列表的数据
			this.getCateList()
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 240rpx;
			border-right: 2rpx solid yellowgreen;

			.left-scroll-view-item {
				// background-color: #eee;
				text-align: center;
				line-height: 60px;
				font-size: 12px;
				border-bottom: 2rpx solid yellowgreen;
				position: relative;
			}
		}
	}

	// 左侧列表点击被激活后的样式
	.active-class {
		background-color: yellowgreen;
		color: white;
	}

	.active-class::before {
		content: '';
		display: block;
		background-color: #c00000;
		width: 3px;
		height: 30px;
		position: absolute;
		left: 0;
		top: 50%;
		transform: translateY(-50%);
	}

	.cate-lev2-title {
		font-size: 30rpx;
		font-weight: bold;
		text-align: center;
		padding: 30rpx 0;
	}

	.cate-lv3-list {
		display: flex;
		flex-wrap: wrap;

		.cate-lv3-item {
			width: 33.3%;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			margin-bottom: 20rpx;

			image {
				height: 120rpx;
				width: 120rpx;
			}

			text {
				font-size: 24rpx;
			}
		}
	}
</style>
